layui.extend({
    selectN: '/layui_extends/selectN',
    selectM: '/layui_extends/selectM',
    baseConfig : "/js/pages/baseConfig"
}).use(['form','layer','table','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;

    var hideCol = true;
    if( baseConfig.checkPrivalege( baseConfig.CONSUMER_LIST_ALL)){
        hideCol = false;
    }
    var tableIns = table.render({
        elem: '#list',
        url : '/sendGoodsRecord/list/',
        text: { none: "Aucun résultat"},
        cellMinWidth : 95,
        height : "full",
        page: true, //开启分页
        limit : 20,
        limits : [20,50,70,100],
        id : "listTable",
        cols : [[
            {field: 'totalAmount', title: "TTC ACHAT", align:"center", templet : function( d){
                return parseFloat( d.totalAmount).toFixed(2);
            }},
            {field: 'createTime', title: 'DATE ENVOIE', align:'center', templet : function( d){
                return baseConfig.formatDate( d.createTime);
            }},
            {field: 'reviewStatus', title: "État", align:"center", templet : function( d){
                if( !d.customsClearanceFormPdf){
                    return "未上传出关单";
                }
                switch( d.reviewStatus) {
                    case "PASS": return "<span style='color: green'>审核通过</span>";break;
                    case "REJECT": return "<span style='color: red'>等待反馈</span>";break;
                    default: return "等待审核";break
                }
            }},
            {field: 'completeSendGoodsNoNumber', title: 'NO. FACTURE', align:'center'},
            {field: 'totalSellAmount', title: 'HT VENTE', align:'center', templet : function( d){
                return parseFloat( d.totalSellAmount).toFixed(2);
            }},/*
            {field: 'uploadCustomsClearanceFormTime', title: '上传出关单时间', align:'center', templet : function( d){
                return baseConfig.formatDate( d.uploadCustomsClearanceFormTime);
            }},*/
            {field: 'reviewMemo', title: "Remarques", align:"center"},
            {title: 'État', fixed: "right", width: 400,align:"center", templet : function ( d) {
                    var str = "";
                    str += "<a class=\"layui-btn layui-btn-xs layui-btn-normal\" lay-event=\"showGoodsList\">出关单填写信息</a>";
                    if( !d.customsClearanceFormPdf){
                        str += "<a class=\"layui-btn layui-btn-xs layui-btn-normal\" lay-event=\"uploadCustomsClearanceForm\">上传出关单</a>";
                    }else{
                        if( d.reviewStatus == "PASS"){
                            str += "<a class=\"layui-btn layui-btn-xs\" lay-event=\"viewCustomsClearanceForm\">查看出关单</a>";
                        }else{
                            if( baseConfig.checkPrivalege( baseConfig.INVOICE_REVIEW)){
                                if( d.reviewStatus != "PASS"){
                                    str += "<a class=\"layui-btn layui-btn-xs layui-btn-warm\" lay-event=\"reviewCustomsClearanceForm\">审核出关单</a>";
                                }
                            }else{
                                str += "<a class=\"layui-btn layui-btn-xs layui-btn-normal\" lay-event=\"uploadCustomsClearanceForm\">重新上传出关单</a>";
                            }
                        }
                    }
                    if( baseConfig.checkPrivalege( baseConfig.INVOICE_REVIEW)) {
                        str += "<a class=\"layui-btn layui-btn-xs layui-btn-normal\" lay-event=\"showGoodsInvoice\">查看发票</a>";
                    }
                    return str;
                }}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.count, //数据条数
                "data": res.data.data //解析数据列表
            };
        }
    });

    //获取浏览器最大高度，以防页面显示不完全
    var maxHeight = $(window).height();

    //打开发货选择商品页面
    function openSendGoods( data){
        var ids = "";
        if( data){
            ids = data.goodsIds;
        }
        var index = layui.layer.open({
            title : "Formalités de dédouanement",
            type : 2,
            area: [1000 + 'px', (maxHeight < 650 ? maxHeight-10 : 650) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "send_choose_goods", //设定一个id，防止重复弹出
            content : "send_choose_goods.html?ids=" + ids,
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                if( ids && ids != ""){
                    body.find( "input[name=totalCount]").val( parseFloat( data.sellTotalAmount).toFixed( 2));
                    body.find( "#number_p").html( data.completeSendGoodsNoNumber);
                }else{
                    body.find( "#content_div").hide();
                }
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }
    $(".send_goods_btn").on("click", function () {
        openSendGoods( null);
    });

    //搜索
    $("input[name=completeSendGoodsNoNumber]").on("input propertychange",function(e){
        search();
    });

    function search(){
        var completeSendGoodsNoNumber = $("input[name=completeSendGoodsNoNumber]").val();
        table.reload("listTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                "completeSendGoodsNoNumber": completeSendGoodsNoNumber
            }
        })
    }

    //excal导出
    $("#download_invoice").on("click",function(){
        var index = layer.msg('导出中，请稍候....',{ icon: 16, time:false, shade:0.8});
        //获取到此时的查询条件
        var completeSendGoodsNoNumber = $("input[name=completeSendGoodsNoNumber]").val();
        $.post( "/sendGoodsRecord/exportInvoice", { "completeSendGoodsNoNumber": completeSendGoodsNoNumber }, function( res){
            layer.close( index);
            if( res.code == 200){
                var exportCount = res.data.exportCount;
                var filePath = res.data.filePath;
                top.layer.alert( "导出成功");
                layer.confirm( "导出成功,共导出[" + exportCount + "]条数据，确认下载文件？", function( confirmIndex){
                    window.open( filePath);
                    layer.close( confirmIndex);
                });
            }else{
                top.layer.alert( "导出失败");
            }
        });
    });
    $("#download_excel").on("click",function(){
        var index = layer.msg('导出中，请稍候....',{ icon: 16, time:false, shade:0.8});
        //获取到此时的查询条件
        var completeSendGoodsNoNumber = $("input[name=completeSendGoodsNoNumber]").val();
        $.post( "/sendGoodsRecord/exportAsExcel", { "completeSendGoodsNoNumber": completeSendGoodsNoNumber}, function( res){
            layer.close( index);
            if( res.code == 200){
                var exportCount = res.data.exportCount;
                var filePath = res.data.filePath;
                top.layer.alert( "导出成功");
                layer.confirm( "导出成功,共导出[" + exportCount + "]条数据，确认下载Excel文件？", function( confirmIndex){
                    window.open( filePath);
                    layer.close( confirmIndex);
                });
            }else{
                top.layer.alert( "导出失败");
            }
        });
    });

    //处理权限相关的东西
    function handlePrivage(){
        //判断是否有查看所有数据的权限，如果没有，则隐藏两个下载按钮
        if( baseConfig.checkPrivalege( baseConfig.GOODS_LIST_ALL)){
            $("#download_invoice").show();
            $("#download_excel").show();
        }else{
            $("#download_invoice").hide();
            $("#download_excel").hide();
        }
    }

    handlePrivage();

    //上传出关单
    function upploadCustomsClearanceForm( data){
        var customsClearanceFormImg = "";
        if( data && data.customsClearanceFormImg){
            customsClearanceFormImg = data.customsClearanceFormImg;
        }
        var index = layui.layer.open({
            title : "上传出关单",
            type : 2,
            area: [850 + 'px', ( maxHeight-10) + 'px'],
            shade: 0.4,
            id: "uploadCustomsClearanceForm", //设定一个id，防止重复弹出
            content : " uploadCustomsClearanceForm.html?customsClearanceFormImg=" + customsClearanceFormImg,
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                body.find( "input[name=id]").val( data.id);
                body.find( "#pdfImg").attr( "src", data.customsClearanceFormImg);
                body.find( "input[name=customsClearanceFormPdf]").val( data.customsClearanceFormPdf);
                body.find( "input[name=customsClearanceFormImg]").val( data.customsClearanceFormImg);
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    function reviewCcustomsClearanceForm( data){
        var customsClearanceFormImg = "";
        if( data && data.customsClearanceFormImg){
            customsClearanceFormImg = data.customsClearanceFormImg;
        }
        var index = layui.layer.open({
            title : "审核出关单",
            type : 2,
            area: [850 + 'px', (maxHeight-10) + 'px'],
            shade: 0.4,
            id: "reviewCustomsClearanceForm", //设定一个id，防止重复弹出
            content : " reviewCustomsClearanceForm.html?customsClearanceFormImg=" + customsClearanceFormImg,
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                body.find( "input[name=id]").val( data.id);
                body.find( "input[name=customsClearanceFormPdf]").val( data.customsClearanceFormPdf);
                body.find( "input[name=customsClearanceFormImg]").val( data.customsClearanceFormImg);
                body.find( "input[name=trackingNumber]").val( data.trackingNumber);
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    function showCustomsClearanceForm( data){
        var customsClearanceFormImg = "";
        if( data && data.customsClearanceFormImg){
            customsClearanceFormImg = data.customsClearanceFormImg;
        }
        var index = layui.layer.open({
            title : "查看出关单",
            type : 2,
            area: [650 + 'px', (maxHeight-10) + 'px'],
            shade: 0.4,
            id: "showCustomsClearanceForm", //设定一个id，防止重复弹出
            content : "showCustomsClearanceForm.html?customsClearanceFormImg=" + customsClearanceFormImg,
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                body.find( "input[name=id]").val( data.id);
                body.find( "input[name=customsClearanceFormPdf]").attr( "src", data.customsClearanceFormPdf);
                body.find( "input[name=customsClearanceFormImg]").attr( "src", data.customsClearanceFormImg);
                body.find( "input[name=trackingNumber]").val( data.trackingNumber);
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'viewCustomsClearanceForm'){//查看商品出关单
            showCustomsClearanceForm( data, false);
        }else if(layEvent === 'reviewCustomsClearanceForm'){//审核商品出关单
            reviewCcustomsClearanceForm( data);
        }else if(layEvent === 'uploadCustomsClearanceForm'){//上传商品出关单
            upploadCustomsClearanceForm( data);
        }else if(layEvent === 'showGoodsInvoice'){//查看本次商品销售对应的发票
            window.open( data.sellInvoicePdf);
        }else if(layEvent === 'showGoodsList'){//查看本次出货商品列表
            openSendGoods( data);
        }
    });
});
